<!DOCTYPE html>
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
  <meta name="description" content="刘清政">
  <meta name="keyword" content="hexo-theme">
  
    <link rel="shortcut icon" href="/css/images/logo.png">
  
  <title>
    
      linux/入门到精通/03-Linux-Shell | Justin-刘清政的博客
    
  </title>
  <link href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/tomorrow.min.css" rel="stylesheet">
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/css/plugins/gitment.css">

  
  <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/geopattern/1.2.3/js/geopattern.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.js"></script>
  
    
<script src="/js/qrious.js"></script>

  
  
    
<script src="/js/gitment.js"></script>

  
  

  
<meta name="generator" content="Hexo 4.2.0"></head>
<div class="wechat-share">
  <img src="/css/images/logo.png" />
</div>

  <body>
    <header class="header fixed-header">
  <div class="header-container">
    <a class="home-link" href="/">
      <div class="logo"></div>
      <span>Justin-刘清政的博客</span>
    </a>
    <ul class="right-list">
      
        <li class="list-item">
          
            <a href="/" class="item-link">主页</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/tags/" class="item-link">标签</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/archives/" class="item-link">归档</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/about/" class="item-link">关于我</a>
          
        </li>
      
    </ul>
    <div class="menu">
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
    </div>
    <div class="menu-mask">
      <ul class="menu-list">
        
          <li class="menu-item">
            
              <a href="/" class="menu-link">主页</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/tags/" class="menu-link">标签</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/archives/" class="menu-link">归档</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/about/" class="menu-link">关于我</a>
            
          </li>
        
      </ul>
    </div>
  </div>
</header>

    <div id="article-banner">
  <h2>linux/入门到精通/03-Linux-Shell</h2>



  <p class="post-date">2020-07-06</p>
    <!-- 不蒜子统计 -->
    <span id="busuanzi_container_page_pv" style='display:none' class="">
        <i class="icon-smile icon"></i> 阅读数：<span id="busuanzi_value_page_pv"></span>次
    </span>
  <div class="arrow-down">
    <a href="javascript:;"></a>
  </div>
</div>
<main class="app-body flex-box">
  <!-- Article START -->
  <article class="post-article">
    <section class="markdown-content"><h3 id="1-什么是Bash-shell-壳"><a href="#1-什么是Bash-shell-壳" class="headerlink" title="1.什么是Bash shell(壳)"></a>1.什么是Bash shell(壳)</h3><p>Bash Shell是一个命令解释器，它在操作系统的最外层，负责用户程序与内核进行交互操作的一种接口，将用户输入的命令翻译给操作系统，并将处理后的结果输出至屏幕。</p>
<p>通过xshell连接，就是打开了一个bash程序的窗口，不能点鼠标，只能输入命令</p>
<p>当我们使用远程连接工具连接linux服务，系统则会给打开一个默认的shell，我们可在这个界面执行命令、比如：获取系统当前时间，创建一个用户等等…</p>
<p><img src="https://tva1.sinaimg.cn/large/007S8ZIlgy1ght13kpua0j30w80gq0vs.jpg" alt="image-20200816223737732"></p>
<h3 id="2-Bash-Shell能干什么"><a href="#2-Bash-Shell能干什么" class="headerlink" title="2.Bash Shell能干什么?"></a>2.Bash Shell能干什么?</h3><p>使用Shell实现对Linux系统的大部分管理，例如:<br>1.文件管理(文件创建，移动，复制，删除，编辑…)<br>2.权限管理(不同用户不通权限)<br>3.用户管理（创建，删除….）<br>4.磁盘管理（挂载）<br>5.网络管理<br>6.软件管理<br>.等等.</p>
<h3 id="3-平时我们如何使用Shell呢？"><a href="#3-平时我们如何使用Shell呢？" class="headerlink" title="3.平时我们如何使用Shell呢？"></a>3.平时我们如何使用Shell呢？</h3><p>输入命令 –&gt; 效率低 –&gt;适合少量的工作<br>Shell Script –&gt; 效率高–&gt;适合复杂重复性的工作<br>例如:创建100个用户，单纯输入命令需要执行100次，而Shell脚本只需要几行命令即可完成100个用户的创建</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">vim test.sh</span><br><span class="line"><span class="keyword">for</span> i <span class="keyword">in</span> &#123;<span class="number">1.</span><span class="number">.100</span>&#125;</span><br><span class="line">do</span><br><span class="line"> useradd test$i</span><br><span class="line"> echo <span class="string">"create test$i"</span></span><br><span class="line">done</span><br><span class="line"></span><br><span class="line">bash test.sh</span><br></pre></td></tr></table></figure>



<h3 id="4-Shell提示符"><a href="#4-Shell提示符" class="headerlink" title="4.Shell提示符"></a>4.Shell提示符</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># $ = 普通用户, # = root用户(超级管理员)</span></span><br><span class="line"><span class="comment">### [root@lqz ~]# </span></span><br><span class="line"><span class="comment"># root:当前登录用户</span></span><br><span class="line"><span class="comment"># @ ：没有意义</span></span><br><span class="line"><span class="comment"># lqz：主机名称，如果很长会显示不全，通过hostname查看</span></span><br><span class="line"><span class="comment"># ~：当前用户所在家目录</span></span><br><span class="line"><span class="comment"># #：通常指超级管理员</span></span><br><span class="line"><span class="comment"># $:普通用户</span></span><br><span class="line">[root@lqz ~]<span class="comment"># whoami</span></span><br><span class="line">root</span><br><span class="line"></span><br><span class="line">[root@web01 ~]<span class="comment"># useradd jack</span></span><br><span class="line">[root@web01 ~]<span class="comment"># passwd jack</span></span><br><span class="line">Changing password <span class="keyword">for</span> user jack.</span><br><span class="line">New password:</span><br><span class="line">BAD PASSWORD: The password is a palindrome</span><br><span class="line">Retype new password:</span><br><span class="line">passwd: all authentication tokens updated successfully.</span><br><span class="line">[root@web01 ~]<span class="comment"># su jack</span></span><br><span class="line">[root@web01 ~]<span class="comment"># cd root   提示没有权限</span></span><br></pre></td></tr></table></figure>

<h3 id="5-Shell基础语法"><a href="#5-Shell基础语法" class="headerlink" title="5.Shell基础语法"></a>5.Shell基础语法</h3><p>命令行bash shell， 为用户提供输入, 执行命令的界面</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#命令  选项  参数</span></span><br><span class="line"><span class="built_in">command</span> [-options] [arguments]</span><br><span class="line"></span><br><span class="line">[root@lqz ~]<span class="comment"># ls             #命令</span></span><br><span class="line">[root@lqz ~]<span class="comment"># ls -a          #命令+选项</span></span><br><span class="line">[root@lqz ~]<span class="comment"># ls -a /home/   #命令+选项+参数</span></span><br><span class="line">ls -a -l</span><br><span class="line">ls -la</span><br><span class="line">ls --all</span><br><span class="line">ls --list</span><br><span class="line">ls -c</span><br><span class="line">ls --color</span><br><span class="line">ls -a /tmp</span><br><span class="line"><span class="comment">#命令: 整条shell命令的主体</span></span><br><span class="line"><span class="comment">#选项: 用于调节命令的输出效果</span></span><br><span class="line">    <span class="comment">#以 “-”引导短格式选项（单个字符），例如“-l”</span></span><br><span class="line">    <span class="comment">#以“--”引导长格式选项（多个字符），例如“--color”</span></span><br><span class="line">    <span class="comment">#多个短格式选项可以写在一起，只用一个“-”引导，例如“-al”</span></span><br><span class="line"><span class="comment">#参数: 命令操作的对象，如文件、目录名等</span></span><br><span class="line"><span class="comment"># 选项和参数可以出现位置调换，命令，选项，参数之间要至少有一个空格</span></span><br><span class="line"><span class="comment"># 命令必须开头， 选项和参数位置可以发生变化</span></span><br></pre></td></tr></table></figure>

<h2 id="2-Bash-Shell基本特性"><a href="#2-Bash-Shell基本特性" class="headerlink" title="2.Bash Shell基本特性"></a>2.Bash Shell基本特性</h2><h3 id="1-命令补全"><a href="#1-命令补全" class="headerlink" title="1.命令补全"></a>1.命令补全</h3><p>当使用windows查找一个目录层级特别多的文件时，打开的效率会非常的慢，但如果使用linux查找一个目录层级特别多的文件时，可以通过tab键快速的补全</p>
<p>PS: tab键可以实现命令补全，路径补全，在实际生产中tab补全往往是我们使用的最多的, 因为可以减少执行命令以及路径出错率。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># yum provides ifconfig</span></span><br><span class="line"><span class="comment"># yum install net-tools</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#查看ip时忘记具体了命令</span></span><br><span class="line">[root@lqz ~]<span class="comment"># ifcon</span></span><br><span class="line"><span class="comment">#按下tab键会自动补全</span></span><br><span class="line">[root@lqz ~]<span class="comment"># ifconfig</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#按一下tab键没有反应, 按两下tab键列出所有if开头的命令</span></span><br><span class="line">[root@lqz ~]<span class="comment"># if</span></span><br><span class="line"><span class="keyword">if</span>         ifconfig   ifenslave  ifrename</span><br><span class="line">ifcfg      ifdown     ifnames    ifup</span><br><span class="line"></span><br><span class="line"><span class="comment">#linux目录较深，经常使用tab键进行补全, 如果路径出错是没有办法补全(带斜线表示是目录（window叫文件夹）)</span></span><br><span class="line">[root@lqz ~]<span class="comment"># ls /etc/sysconfig/network-scripts/</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 安装选项补全</span></span><br><span class="line">yum install bash-comple*</span><br><span class="line">yum install bash-completion </span><br><span class="line"><span class="comment"># ls -- Tab建，空格上下翻</span></span><br></pre></td></tr></table></figure>

<h3 id="2-命令快捷键，快捷键可以帮助我们大大提升工作效率"><a href="#2-命令快捷键，快捷键可以帮助我们大大提升工作效率" class="headerlink" title="2.命令快捷键，快捷键可以帮助我们大大提升工作效率"></a>2.命令快捷键，快捷键可以帮助我们大大提升工作效率</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">Ctrl + a    <span class="comment">#光标跳转至正在输入的命令行的首部</span></span><br><span class="line">Ctrl + e    <span class="comment">#光标跳转至正在输入的命令行的尾部</span></span><br><span class="line">Ctrl + c    <span class="comment">#终止前台运行的程序</span></span><br><span class="line">Ctrl + d    <span class="comment">#在shell中，ctrl-d表示推出当前shell。</span></span><br><span class="line">Ctrl + z    <span class="comment">#将任务暂停，挂至后台</span></span><br><span class="line">Ctrl + l    <span class="comment">#清屏，和clear命令等效。</span></span><br><span class="line">Ctrl + k    <span class="comment">#删除从光标到行末的所有字符</span></span><br><span class="line">Ctrl + u    <span class="comment">#删除从光标到行首的所有字符</span></span><br><span class="line">Ctrl + r    <span class="comment">#搜索历史命令, 利用关键字，Tab建选中</span></span><br><span class="line">Ctrl + w    <span class="comment">#按单词或空格进行向前删除</span></span><br><span class="line">Ctrl + 左右建 <span class="comment">#按单词或空格进行向前向后跳</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#在命令行前加面加 "#" 则该命令不会被执行</span></span><br></pre></td></tr></table></figure>

<h3 id="3-历史命令History-追溯之前发生情况"><a href="#3-历史命令History-追溯之前发生情况" class="headerlink" title="3.历史命令History, 追溯之前发生情况"></a>3.历史命令History, 追溯之前发生情况</h3><p>-w 保存命令历史到历史文件<br>-c 清空命令历史记录, 不会情况文件<br>-d 删除命令历史的第 N 条行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#1.使用双 !! 可执行上一条执行过的命令</span></span><br><span class="line">[root@lqz ~]<span class="comment"># ls</span></span><br><span class="line">lqz.com</span><br><span class="line">[root@lqz ~]<span class="comment"># !!</span></span><br><span class="line">ls</span><br><span class="line">lqz.com</span><br><span class="line"></span><br><span class="line"><span class="comment">#2.输入!6, 执行history命令历史中第 6 行命令</span></span><br><span class="line">[root@lqz ~]<span class="comment"># !6</span></span><br><span class="line">touch lqz.com</span><br><span class="line"></span><br><span class="line"><span class="comment">#使用!cat, 调用history命令历史最近一次执行过的cat命令</span></span><br><span class="line">[root@lqz ~]<span class="comment"># cat /etc/sysconfig/network-scripts/ifcfg-eth0</span></span><br><span class="line">[root@lqz ~]<span class="comment"># !cat  # 最近一次</span></span><br><span class="line">cat /etc/sysconfig/network-scripts/ifcfg-eth0</span><br><span class="line"></span><br><span class="line">[root@lqz ~]<span class="comment"># ls /etc/passwd</span></span><br><span class="line"><span class="comment">#调用上一条命令的最后参数或选项, 按下ESC松开, 然后按下 "."</span></span><br><span class="line">[root@lqz ~]<span class="comment"># cat</span></span><br><span class="line">[root@lqz ~]<span class="comment"># cat /etc/passwd</span></span><br><span class="line"><span class="comment">#第二种方式, 输入!$</span></span><br><span class="line">[root@lqz ~]<span class="comment"># ls !$</span></span><br><span class="line">ls /etc/passwd</span><br><span class="line">/etc/passwd</span><br><span class="line"></span><br><span class="line"><span class="comment"># history -d 100  删除第100条历史记录</span></span><br><span class="line"><span class="comment"># history -c      清空记录(保存到文件中的还有)</span></span><br><span class="line"><span class="comment"># history -w      保存历史集合，保存到当前用户的家目录 .bash_history</span></span><br><span class="line"><span class="comment"># &gt; .bash_history  清空文件的保存（操作完，不让别人看你执行的命令）</span></span><br></pre></td></tr></table></figure>

<h3 id="3-命令别名"><a href="#3-命令别名" class="headerlink" title="3.命令别名"></a>3.命令别名</h3><p>命令别名将用户经常使用的复杂命令简单化, 可以用<code>&quot;alias 别名名称=命令&quot;</code>命令创建属于自己的命令别名, 若要取消一个命令别名，则是用<code>unalias 别名名称</code>命令。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#1.定义临时别名, wk为查看eth0网卡别名</span></span><br><span class="line">[root@lqz ~]<span class="comment"># alias wk='ifconfig'</span></span><br><span class="line"><span class="comment"># alias 查看一下是否成功，发现系统内置了很多</span></span><br><span class="line">[root@lqz ~]<span class="comment"># wk</span></span><br><span class="line"><span class="comment"># 取消别名（都是临时的）</span></span><br><span class="line">[root@lqz ~]<span class="comment"># unalias wk </span></span><br><span class="line"></span><br><span class="line"><span class="comment">#2.如果定义命令本身, 会执行什么?</span></span><br><span class="line">[root@lqz ~]<span class="comment"># alias ifconfig='ifconfig eth0'</span></span><br><span class="line"><span class="comment">#绝对路径执行, 调用命令本身</span></span><br><span class="line">[root@lqz ~]<span class="comment"># /sbin/ifconfig</span></span><br><span class="line"><span class="comment">#通过转义字符, 调用命令本身</span></span><br><span class="line">[root@WebServer ~]<span class="comment"># ifconfig</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#3.取消别名</span></span><br><span class="line">[root@lqz ~]<span class="comment"># unalias ifconfig</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#4.永久生效，/etc/bashrc</span></span><br><span class="line">[root@lqz ~]<span class="comment"># echo "alias ifconfig='ifconfig eth0'" &gt;&gt; /etc/bashrc</span></span><br></pre></td></tr></table></figure>

<h2 id="3-Bash-Shell获取帮助"><a href="#3-Bash-Shell获取帮助" class="headerlink" title="3.Bash Shell获取帮助"></a>3.Bash Shell获取帮助</h2><h3 id="1-命令-–help帮助"><a href="#1-命令-–help帮助" class="headerlink" title="1.命令 –help帮助"></a>1.命令 –help帮助</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">[root@lqz ~]<span class="comment"># ls --help</span></span><br><span class="line">用法: ls [选项]...[文件]...</span><br><span class="line"></span><br><span class="line"><span class="comment">#ls 常见选项</span></span><br><span class="line">-a  <span class="comment">#查看目录下的所有文件，包括隐藏文件</span></span><br><span class="line">-l  <span class="comment">#以长格式的方式显示文件的详细内容</span></span><br><span class="line">-h  <span class="comment">#以人性化的方式显示内容，配合-l使用</span></span><br><span class="line">-d  <span class="comment">#只列出目录名，不列出目录以下的内容</span></span><br><span class="line">-t  <span class="comment">#按修改时间进行排序</span></span><br><span class="line">-i  <span class="comment">#显示文件的inode(该文件在该分区的一个编号)</span></span><br><span class="line"></span><br><span class="line">[root@lqz ~]<span class="comment"># date --help</span></span><br><span class="line">Usage: date [OPTION]... [+FORMAT]</span><br><span class="line">  or:  date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]</span><br><span class="line">  </span><br><span class="line">[root@lqz ~]<span class="comment"># date +%F   #查看日期</span></span><br><span class="line">2019-03-28</span><br><span class="line">[root@lqz ~]<span class="comment"># date -s 12:00  #修改时间</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#时间不是用来查看，而是有其他的用途</span></span><br><span class="line">[root@web01 ~]<span class="comment"># touch `date +%F`_file.txt</span></span><br><span class="line">[root@web01 ~]<span class="comment"># ls</span></span><br><span class="line">2019-03-28_file.txt</span><br></pre></td></tr></table></figure>

<h3 id="2-命令-man手册"><a href="#2-命令-man手册" class="headerlink" title="2.命令 man手册"></a>2.命令 man手册</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># man ls    #查看ls命令的手册</span></span><br></pre></td></tr></table></figure>

<p>3.linux命令大全url传送门<br><a href="http://man.linuxde.net/" target="_blank" rel="noopener">linux命令大全</a><br><a href="http://linux.51yip.com/" target="_blank" rel="noopener">linux命令手册</a></p>
<h2 id="4-Bash-Shell命令流程"><a href="#4-Bash-Shell命令流程" class="headerlink" title="4.Bash Shell命令流程"></a>4.Bash Shell命令流程</h2><h3 id="1-当我们执行一个命令-整个命令执行流程如下"><a href="#1-当我们执行一个命令-整个命令执行流程如下" class="headerlink" title="1.当我们执行一个命令, 整个命令执行流程如下:"></a>1.当我们执行一个命令, 整个命令执行流程如下:</h3><p>1) 判断命令是否通过绝对路径执行<br>2) 判断命令是否存在alias别名<br>3) 判断用户输入的是内部命令还是外部命令<br>4) Bash内部命令直接执行，外部命令检测是否存在缓存<br>5) 通过PATH路径查找命令，有执行，无报错</p>
<h3 id="2-什么是内部命令，什么是外部命令"><a href="#2-什么是内部命令，什么是外部命令" class="headerlink" title="2.什么是内部命令，什么是外部命令"></a>2.什么是内部命令，什么是外部命令</h3><p>内部命令: shell程序自带的命令。<br>外部命令: 在系统PATH变量的某个路径下的可执行程序。</p>
<h3 id="3-如何检查用户输入的命令是内部命令还是外部命令"><a href="#3-如何检查用户输入的命令是内部命令还是外部命令" class="headerlink" title="3.如何检查用户输入的命令是内部命令还是外部命令"></a>3.如何检查用户输入的命令是内部命令还是外部命令</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#cd命令属于shell内部命令</span></span><br><span class="line">[root@linux-node1 ~]<span class="comment"># type -a cd</span></span><br><span class="line"><span class="built_in">cd</span> is a shell <span class="built_in">builtin</span></span><br><span class="line"><span class="built_in">cd</span> is /usr/bin/<span class="built_in">cd</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#ping属于外部命令, 同时会打印当前命令路径</span></span><br><span class="line">[root@lqz ~]<span class="comment"># type -a  ping</span></span><br><span class="line">ping is /bin/ping</span><br></pre></td></tr></table></figure>

<h3 id="4-如果是外置命令，Bash可以通过查找PATH变量，获取该命令的绝对路径。"><a href="#4-如果是外置命令，Bash可以通过查找PATH变量，获取该命令的绝对路径。" class="headerlink" title="4.如果是外置命令，Bash可以通过查找PATH变量，获取该命令的绝对路径。"></a>4.如果是外置命令，Bash可以通过查找PATH变量，获取该命令的绝对路径。</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#打印当前环境变量目录</span></span><br><span class="line">[root@lqz ~]<span class="comment"># echo $PATH</span></span><br><span class="line">/usr/<span class="built_in">local</span>/sbin:/usr/<span class="built_in">local</span>/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin</span><br></pre></td></tr></table></figure>

<p>PS: PATH由多个路径组成，每个路径值之间用冒号间隔，对这些路径的增加和删除操作都将影响到Bash解释器对Linux命令的查找</p>
<h3 id="5-如果是外置命令还会涉及到一个内存缓存，也就是说，当我们出现重复执行相同的命令，会通过缓存调取执行，也就意味着不会搜索PATH路径。"><a href="#5-如果是外置命令还会涉及到一个内存缓存，也就是说，当我们出现重复执行相同的命令，会通过缓存调取执行，也就意味着不会搜索PATH路径。" class="headerlink" title="5.如果是外置命令还会涉及到一个内存缓存，也就是说，当我们出现重复执行相同的命令，会通过缓存调取执行，也就意味着不会搜索PATH路径。"></a>5.如果是外置命令还会涉及到一个内存缓存，也就是说，当我们出现重复执行相同的命令，会通过缓存调取执行，也就意味着不会搜索PATH路径。</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#表缓存命令所在位置</span></span><br><span class="line">[root@lqz ~]<span class="comment"># hash</span></span><br><span class="line">hits    <span class="built_in">command</span></span><br><span class="line">   1    /usr/bin/tty</span><br><span class="line">   3    /sbin/ifconfig</span><br><span class="line"></span><br><span class="line"><span class="comment">#已缓存命令,如果移动位置会导致无法找到该命令</span></span><br><span class="line">[root@lqz ~]<span class="comment"># mv /sbin/ifconfig /bin/</span></span><br><span class="line">[root@lqz ~]<span class="comment"># ifconfig</span></span><br><span class="line">-bash: /sbin/ifconfig: No such file or directory</span><br><span class="line"></span><br><span class="line"><span class="comment">#删除缓存过的ifconfig命令, 即可执行</span></span><br><span class="line">[root@lqz ~]<span class="comment"># hash -d ifconfig</span></span><br><span class="line">[root@lqz ~]<span class="comment"># ifconfig</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#当然可以清空缓存表</span></span><br><span class="line">[root@lqz ~]<span class="comment"># hash -r</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#注意: 命令缓存hash需要注意如下情况:</span></span><br><span class="line">1.只要执行外部命令1次就会对该命令进行缓存</span><br><span class="line">2.如果将命令移动了位置，该如何执行</span><br><span class="line">    a.使用绝对路径执行</span><br><span class="line">    b.删除<span class="built_in">hash</span>表的缓存指令</span><br></pre></td></tr></table></figure>

<h3 id="6-命令执行流程总结-当我们执行了一个ping命令之后-整个命令执行的流程步骤如下"><a href="#6-命令执行流程总结-当我们执行了一个ping命令之后-整个命令执行的流程步骤如下" class="headerlink" title="6.命令执行流程总结: 当我们执行了一个ping命令之后, 整个命令执行的流程步骤如下:"></a>6.命令执行流程总结: 当我们执行了一个<code>ping</code>命令之后, 整个命令执行的流程步骤如下:</h3><p>1) 检查执行的命令是否使用的是绝对路径执行的。<br>2) 检查ping命令是否存在alias别名<br>3) 检查ping命令是内部命令还是外部命令<br>4) 如果是内部命令Bash直接执行，如果是外部命令，首先检查Hash缓存，存在则直接调取<br>5) 如果该命令不存在Hash缓存，则通过PATH路径进行逐行查找该命令所在的位置<br>6) 如果PATH路径没有查找到该命令所在的路径，则返回错误码。command not found</p>
</section>
    <!-- Tags START -->
    
    <!-- Tags END -->
    <!-- NAV START -->
    
  <div class="nav-container">
    <!-- reverse left and right to put prev and next in a more logic postition -->
    
      <a class="nav-left" href="/python/Django-rest-framework%E6%A1%86%E6%9E%B6/7-drf-%E8%BF%87%E6%BB%A4%E6%8E%92%E5%BA%8F%E5%88%86%E9%A1%B5%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86/">
        <span class="nav-arrow">← </span>
        
          python/Django-rest-framework框架/7-drf-过滤排序分页异常处理
        
      </a>
    
    
      <a class="nav-right" href="/linux/%E5%85%A5%E9%97%A8%E5%88%B0%E7%B2%BE%E9%80%9A/04-Linux%E6%96%87%E4%BB%B6%E7%AE%A1%E7%90%86/">
        
          linux/入门到精通/04-Linux文件管理
        
        <span class="nav-arrow"> →</span>
      </a>
    
  </div>

    <!-- NAV END -->
    <!-- 打赏 START -->
    
      <div class="money-like">
        <div class="reward-btn">
          赏
          <span class="money-code">
            <span class="alipay-code">
              <div class="code-image"></div>
              <b>使用支付宝打赏</b>
            </span>
            <span class="wechat-code">
              <div class="code-image"></div>
              <b>使用微信打赏</b>
            </span>
          </span>
        </div>
        <p class="notice">点击上方按钮,请我喝杯咖啡！</p>
      </div>
    
    <!-- 打赏 END -->
    <!-- 二维码 START -->
    
      <div class="qrcode">
        <canvas id="share-qrcode"></canvas>
        <p class="notice">扫描二维码，分享此文章</p>
      </div>
    
    <!-- 二维码 END -->
    
      <!-- Gitment START -->
      <div id="comments"></div>
      <!-- Gitment END -->
    
  </article>
  <!-- Article END -->
  <!-- Catalog START -->
  
    <aside class="catalog-container">
  <div class="toc-main">
  <!-- 不蒜子统计 -->
    <strong class="toc-title">目录</strong>
    
      <ol class="toc-nav"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#1-什么是Bash-shell-壳"><span class="toc-nav-text">1.什么是Bash shell(壳)</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#2-Bash-Shell能干什么"><span class="toc-nav-text">2.Bash Shell能干什么?</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#3-平时我们如何使用Shell呢？"><span class="toc-nav-text">3.平时我们如何使用Shell呢？</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#4-Shell提示符"><span class="toc-nav-text">4.Shell提示符</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#5-Shell基础语法"><span class="toc-nav-text">5.Shell基础语法</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#2-Bash-Shell基本特性"><span class="toc-nav-text">2.Bash Shell基本特性</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#1-命令补全"><span class="toc-nav-text">1.命令补全</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#2-命令快捷键，快捷键可以帮助我们大大提升工作效率"><span class="toc-nav-text">2.命令快捷键，快捷键可以帮助我们大大提升工作效率</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#3-历史命令History-追溯之前发生情况"><span class="toc-nav-text">3.历史命令History, 追溯之前发生情况</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#3-命令别名"><span class="toc-nav-text">3.命令别名</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#3-Bash-Shell获取帮助"><span class="toc-nav-text">3.Bash Shell获取帮助</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#1-命令-–help帮助"><span class="toc-nav-text">1.命令 –help帮助</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#2-命令-man手册"><span class="toc-nav-text">2.命令 man手册</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#4-Bash-Shell命令流程"><span class="toc-nav-text">4.Bash Shell命令流程</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#1-当我们执行一个命令-整个命令执行流程如下"><span class="toc-nav-text">1.当我们执行一个命令, 整个命令执行流程如下:</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#2-什么是内部命令，什么是外部命令"><span class="toc-nav-text">2.什么是内部命令，什么是外部命令</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#3-如何检查用户输入的命令是内部命令还是外部命令"><span class="toc-nav-text">3.如何检查用户输入的命令是内部命令还是外部命令</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#4-如果是外置命令，Bash可以通过查找PATH变量，获取该命令的绝对路径。"><span class="toc-nav-text">4.如果是外置命令，Bash可以通过查找PATH变量，获取该命令的绝对路径。</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#5-如果是外置命令还会涉及到一个内存缓存，也就是说，当我们出现重复执行相同的命令，会通过缓存调取执行，也就意味着不会搜索PATH路径。"><span class="toc-nav-text">5.如果是外置命令还会涉及到一个内存缓存，也就是说，当我们出现重复执行相同的命令，会通过缓存调取执行，也就意味着不会搜索PATH路径。</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#6-命令执行流程总结-当我们执行了一个ping命令之后-整个命令执行的流程步骤如下"><span class="toc-nav-text">6.命令执行流程总结: 当我们执行了一个ping命令之后, 整个命令执行的流程步骤如下:</span></a></li></ol>
    
  </div>
</aside>
  
  <!-- Catalog END -->
</main>

<script>
  (function () {
    var url = 'http://www.liuqingzheng.top/linux/入门到精通/03-Linux-Shell/';
    var banner = ''
    if (banner !== '' && banner !== 'undefined' && banner !== 'null') {
      $('#article-banner').css({
        'background-image': 'url(' + banner + ')'
      })
    } else {
      $('#article-banner').geopattern(url)
    }
    $('.header').removeClass('fixed-header')

    // error image
    $(".markdown-content img").on('error', function() {
      $(this).attr('src', 'http://file.muyutech.com/error-img.png')
      $(this).css({
        'cursor': 'default'
      })
    })

    // zoom image
    $(".markdown-content img").on('click', function() {
      var src = $(this).attr('src')
      if (src !== 'http://file.muyutech.com/error-img.png') {
        var imageW = $(this).width()
        var imageH = $(this).height()

        var zoom = ($(window).width() * 0.95 / imageW).toFixed(2)
        zoom = zoom < 1 ? 1 : zoom
        zoom = zoom > 2 ? 2 : zoom
        var transY = (($(window).height() - imageH) / 2).toFixed(2)

        $('body').append('<div class="image-view-wrap"><div class="image-view-inner"><img src="'+ src +'" /></div></div>')
        $('.image-view-wrap').addClass('wrap-active')
        $('.image-view-wrap img').css({
          'width': `${imageW}`,
          'transform': `translate3d(0, ${transY}px, 0) scale3d(${zoom}, ${zoom}, 1)`
        })
        $('html').css('overflow', 'hidden')

        $('.image-view-wrap').on('click', function() {
          $(this).remove()
          $('html').attr('style', '')
        })
      }
    })
  })();
</script>


  <script>
    var qr = new QRious({
      element: document.getElementById('share-qrcode'),
      value: document.location.href
    });
  </script>



  <script>
    var gitmentConfig = "liuqingzheng";
    if (gitmentConfig !== 'undefined') {
      var gitment = new Gitment({
        id: "linux/入门到精通/03-Linux-Shell",
        owner: "liuqingzheng",
        repo: "FuckBlog",
        oauth: {
          client_id: "32a4076431cf39d0ecea",
          client_secret: "94484bd79b3346a949acb2fda3c8a76ce16990c6"
        },
        theme: {
          render(state, instance) {
            const container = document.createElement('div')
            container.lang = "en-US"
            container.className = 'gitment-container gitment-root-container'
            container.appendChild(instance.renderHeader(state, instance))
            container.appendChild(instance.renderEditor(state, instance))
            container.appendChild(instance.renderComments(state, instance))
            container.appendChild(instance.renderFooter(state, instance))
            return container;
          }
        }
      })
      gitment.render(document.getElementById('comments'))
    }
  </script>




    <div class="scroll-top">
  <span class="arrow-icon"></span>
</div>
    <footer class="app-footer">
<!-- 不蒜子统计 -->
<span id="busuanzi_container_site_pv">
     本站总访问量<span id="busuanzi_value_site_pv"></span>次
</span>
<span class="post-meta-divider">|</span>
<span id="busuanzi_container_site_uv" style='display:none'>
     本站访客数<span id="busuanzi_value_site_uv"></span>人
</span>
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>



  <p class="copyright">
    &copy; 2021 | Proudly powered by <a href="https://www.cnblogs.com/xiaoyuanqujing" target="_blank">小猿取经</a>
    <br>
    Theme by <a href="https://www.cnblogs.com/xiaoyuanqujing" target="_blank" rel="noopener">小猿取经</a>
  </p>
</footer>

<script>
  function async(u, c) {
    var d = document, t = 'script',
      o = d.createElement(t),
      s = d.getElementsByTagName(t)[0];
    o.src = u;
    if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
    s.parentNode.insertBefore(o, s);
  }
</script>
<script>
  async("//cdnjs.cloudflare.com/ajax/libs/fastclick/1.0.6/fastclick.min.js", function(){
    FastClick.attach(document.body);
  })
</script>

<script>
  var hasLine = 'true';
  async("//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js", function(){
    $('figure pre').each(function(i, block) {
      var figure = $(this).parents('figure');
      if (hasLine === 'false') {
        figure.find('.gutter').hide();
      }
      var lang = figure.attr('class').split(' ')[1] || 'code';
      var codeHtml = $(this).html();
      var codeTag = document.createElement('code');
      codeTag.className = lang;
      codeTag.innerHTML = codeHtml;
      $(this).attr('class', '').empty().html(codeTag);
      figure.attr('data-lang', lang.toUpperCase());
      hljs.highlightBlock(block);
    });
  })
</script>





<!-- Baidu Tongji -->

<script>
    var _baId = 'c5fd96eee1193585be191f318c3fa725';
    // Originial
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?" + _baId;
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
</script>


<script src="/js/script.js"></script>


<script src="/js/search.js"></script>


<script src="/js/load.js"></script>



  <span class="local-search local-search-google local-search-plugin" style="right: 50px;top: 70px;;position:absolute;z-index:2;">
      <input type="search" placeholder="站内搜索" id="local-search-input" class="local-search-input-cls" style="">
      <div id="local-search-result" class="local-search-result-cls"></div>
  </span>


  </body>
</html>